﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region using
using System;
using System.Configuration;
using System.IO;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;

#endregion


namespace Rio.WCF.Services
{
    public partial class Treatments
    {
        #region Public method
        
        /// <summary>
        /// Check if the directory exists. If not create it.
        /// </summary>
        /// <param name="p_sDirectoryFullPath">Full path of te directory</param>
        public static bool CheckDirectory(string p_sDirectoryFullPath)
        {
            if (Directory.Exists(p_sDirectoryFullPath)) return true;
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogInfo("--------------------------------- New Directory created [" + p_sDirectoryFullPath + "] --------------------------- ");
                Directory.CreateDirectory(p_sDirectoryFullPath);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Check if the directory exists. If not create it.
        /// </summary>
        /// <param name="p_sDirectoryFullPath">Full path of te directory</param>
        public static void DeleteDirectory(string p_sDirectoryFullPath)
        {
            if (!Directory.Exists(p_sDirectoryFullPath)) return;
            NetBay.Core.Log.LogManager.Instance().LogInfo("--------------------------------- New Directory DELETED [" + p_sDirectoryFullPath + "] --------------------------- ");
            try
            {
                Directory.Delete(p_sDirectoryFullPath, true);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteDirectory(treatments) EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
            }
        }


        public static void DeleteFile(string p_sFileFullPath)
        {
            if (!File.Exists(p_sFileFullPath)) return;
            NetBay.Core.Log.LogManager.Instance().LogInfo("--------------------------------- Deleting File [" + p_sFileFullPath + "] --------------------------- ");
            try
            {
                File.Delete(p_sFileFullPath);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteFile() EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
            }
        }


        /// <summary>
        /// Create a temporary directory
        /// </summary>
        /// <returns>The full path of the temporary directory</returns>
        public static string CreateTemporaryDirectory()
        {
            NetBay.Core.Log.LogManager.ConfigFile(ConfigurationManager.AppSettings.Get("Rio.WCF.Services.Configuration"));
            NetBay.Core.Log.LogManager.Instance().LogInfo(string.Empty);
            NetBay.Core.Log.LogManager.Instance().LogInfo("---------------------------Call of CreateTemporaryDirectory");

            var l_sCachedirectory = ConfigurationManager.AppSettings.Get("Rio.WCF.Services.TemporaryDirectory");
            CheckDirectory(l_sCachedirectory);

            var l_sGuid = Guid.NewGuid().ToString("N");
            var l_sTempDir = Path.Combine(l_sCachedirectory, l_sGuid);
            NetBay.Core.Log.LogManager.Instance().LogDebug("---------------------------- Creating temporary Directory [" + l_sTempDir + "]");
            try
            {
                CheckDirectory(l_sTempDir);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in CreateTemporaryDirectory()", ex.InnerException);
            }
            NetBay.Core.Log.LogManager.Instance().LogInfo("---------------------------End of Call of CreateTemporaryDirectory");
            return l_sTempDir;

        }

        /// <summary>
        /// Deletes all import cache files.
        /// </summary>
        /// <param name="p_luserId">The p_luser id.</param>
        /// <param name="p_sOrigianlFileName">Name of the P_S origianl file.</param>
        public static void DeleteAllImportCacheFiles(long p_luserId, string p_sOrigianlFileName)
        {
            try
            {
                var l_oDi = new DirectoryInfo(Path.Combine(ConfigurationManager.AppSettings.Get("Rio.WCF.Services.TemporaryDirectory"), p_luserId.ToString()));
                if (l_oDi.Exists)
                {
                    FileInfo[] l_oFileInfo = l_oDi.GetFiles("*_" + p_sOrigianlFileName + Constant.ImageFileFormat.JPG.szGetDescription());

                    foreach (var l_oOneFileInfo in l_oFileInfo)
                    {
                        try
                        {
                            Treatments.DeleteFile(l_oOneFileInfo.FullName);
                        }
                        catch (Exception ex)
                        {
                            NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteAllCacheFiles EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteAllCacheFiles EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
            }
        }

        /// <summary>
        /// Deletes all cache files.
        /// </summary>
        /// <param name="p_sOrigianlFileName">Name of the P_S origianl file.</param>
        public static void DeleteAllCacheFiles(string p_sOrigianlFileName)
        {
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogInfo("------------- Deleting cache file for file: [" + p_sOrigianlFileName + "] " + DateTime.Now);
                var l_oDi = new DirectoryInfo(ConfigurationManager.AppSettings.Get("Rio.WCF.Services.TemporaryDirectory"));
                if (l_oDi.Exists)
                {
                    FileInfo[] l_oFileInfo = l_oDi.GetFiles("*_" + p_sOrigianlFileName + Constant.ImageFileFormat.JPG.szGetDescription());

                    foreach (var l_oOneFileInfo in l_oFileInfo)
                    {
                        try
                        {
                            Treatments.DeleteFile(l_oOneFileInfo.FullName);
                        }
                        catch (Exception ex)
                        {
                            NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteAllCacheFiles EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("------------- EXCEPTION IN DeleteAllCacheFiles EXCEPTION: [" + ex.Message + "] " + DateTime.Now);
            }
        }



        #endregion
    }
}